#!/usr/bin/env python3
"""
自定义HTTP服务器，添加防缓存响应头
"""
import http.server
import socketserver
import os

class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    """自定义请求处理器，添加防缓存响应头"""
    def end_headers(self):
        # 添加防缓存响应头
        self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
        self.send_header('Pragma', 'no-cache')
        self.send_header('Expires', '0')
        super().end_headers()

if __name__ == '__main__':
    # 设置工作目录
    web_dir = os.path.join(os.path.dirname(__file__), '.')
    os.chdir(web_dir)

    # 配置服务器
    PORT = 8080
    handler = NoCacheHTTPRequestHandler
    with socketserver.TCPServer(('', PORT), handler) as httpd:
        print(f'Serving HTTP on port {PORT} with no-cache headers...')
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print('Shutting down server...')
            httpd.server_close()